package cn.com.lms.business.examRewrite.service;

import cn.com.lms.business.core.constant.ErrorEnum;
import cn.com.lms.business.examRewrite.persistence.entity.QuestionCategoryReWrite;
import cn.com.lms.business.examRewrite.persistence.repository.QuestionCategoryReWriteDao;
import com.bnzj.core.exception.BusinessException;
import com.bnzj.core.persistence.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class QuestionCategoryReWriteService extends BaseService<QuestionCategoryReWriteDao,QuestionCategoryReWrite,Long> {
    @Autowired
    QuestionCategoryReWriteService questionCategoryReWriteService;


    /**
     * 获取树列表
     * @return
     */
    public List<QuestionCategoryReWrite> getMenuTree() {
        List<QuestionCategoryReWrite> questionCategoryReWriteServiceAll = questionCategoryReWriteService.findAll(Sort.by(Sort.Direction.ASC, "id"));
        List<QuestionCategoryReWrite> list = questionCategoryReWriteService.generateTreeMenu(questionCategoryReWriteServiceAll);
        return list;
    }

    /**
     * 获取父节点
     * @param questionCategoryReWriteServiceAll
     * @return
     */
    private List<QuestionCategoryReWrite> generateTreeMenu(List<QuestionCategoryReWrite> questionCategoryReWriteServiceAll) {
        List<QuestionCategoryReWrite> collect = questionCategoryReWriteServiceAll.stream().filter(questionCategoryReWrite ->
                questionCategoryReWrite.getIsDelete() == false && QuestionCategoryReWrite.getRoot().getId().equals(questionCategoryReWrite.getParentId())
        ).map(node -> {
            this.findChildren(node, questionCategoryReWriteServiceAll);
            return node;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取子节点
     * @param node
     * @param questionCategoryReWriteServiceAll
     */
    private void findChildren(QuestionCategoryReWrite node, List<QuestionCategoryReWrite> questionCategoryReWriteServiceAll) {
        List<QuestionCategoryReWrite> collect = questionCategoryReWriteServiceAll.stream().filter(questionCategoryReWrite ->
            questionCategoryReWrite.getIsDelete() == false && node.getId().equals(questionCategoryReWrite.getParentId())
        ).collect(Collectors.toList());
        node.setChildren(collect);
        if (collect != null){
            collect.stream().map(item->{
                this.findChildren(node, questionCategoryReWriteServiceAll);
                return item;
            }).collect(Collectors.toList());
        }
    }

    /**
     * 获取指定父类id
     * @param id
     * @return
     */
    public List<Long> findChildrenIds(Long id) {
        QuestionCategoryReWrite root = null;
        if (id==0){
            root = QuestionCategoryReWrite.getRoot();
        }else {
            Optional<QuestionCategoryReWrite> questionCategoryReWrite = findById(id);
            if (!questionCategoryReWrite.isPresent()) {
                throw new BusinessException(ErrorEnum.RECORD_NOT_EXIST.getMessage(), "" + ErrorEnum.RECORD_NOT_EXIST.getCode());
            }
            root = questionCategoryReWrite.get();
        }
        //获取所有的数据
        List<QuestionCategoryReWrite> all = repository.findAll(Sort.by(Sort.Direction.ASC, "id"));
        List<Long> ChildrenIds = new ArrayList<>();
        this.setChildrenIds(root,all,ChildrenIds);
        return ChildrenIds;
    }

    /**
     * 获取指定父类下的子节点
     * @param root
     * @param all
     * @param childrenIds
     */
    private void setChildrenIds(QuestionCategoryReWrite root, List<QuestionCategoryReWrite> all, List<Long> childrenIds) {
        List<QuestionCategoryReWrite> collect = all.stream().filter(item ->
                item.getIsDelete() == false && root.getId().equals(item.getParentId())
        ).collect(Collectors.toList());
        if (collect!=null){
            collect.stream().forEach(item->{
                childrenIds.add(item.getId());
                setChildrenIds(item,all,childrenIds);
            });
        }
    }
}
